package cn.edu.sgu.www.pms.controller;

import cn.edu.sgu.www.pms.annotation.FeignService;
import cn.edu.sgu.www.pms.consts.CustomConstants;
import cn.edu.sgu.www.pms.consts.CustomMessages;
import cn.edu.sgu.www.pms.consts.MimeTypes;
import cn.edu.sgu.www.pms.entity.Table;
import cn.edu.sgu.www.pms.restful.JsonResult;
import cn.edu.sgu.www.pms.service.TableService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "数据库表管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/table", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class TableController {

    private final TableService tableService;

    @Autowired
    public TableController(TableService tableService) {
        this.tableService = tableService;
    }

    @FeignService
    @ApiOperation("通过数据库名查询数据库所有表的名称")
    @RequestMapping(value = "/getTableNames", method = RequestMethod.GET)
    public JsonResult<List<String>> getTableNames(
            @ApiParam(name = "databaseName", value = "数据库名", required = true)
            @NotBlank(message = "数据库名" + CustomMessages.NOT_NULL) String databaseName) {
        List<String> list = tableService.getTableNames(databaseName);

        return JsonResult.success(null, list);
    }

    @FeignService
    @ApiOperation("根据数据库名查询数据库表")
    @RequestMapping(value = "/selectByDatabase", method = RequestMethod.GET)
    public JsonResult<List<Table>> selectByDatabase(
            @ApiParam(name = "databaseName", value = "数据库名", required = true)
            @NotBlank(message = "数据库名" + CustomMessages.NOT_NULL) String databaseName) {
        List<Table> list = tableService.selectByDatabase(databaseName);

        return JsonResult.success(null, list);
    }

}